package com.qf.juc.day01;

import java.util.concurrent.*;

/**
 * 多线程四种写法
 * @author lixu
 */
public class ThreadDemo {


    public static void main(String[] args) throws Exception{
        //1:
        //new MyThread("AA").start();//Thread-0
        //2:
        //new Thread(new MyRunnable(),"BB").start();
        //3:
        //中间类   中间人
        FutureTask<String> futureTask = new FutureTask<>(new MyCallable());
        new Thread(futureTask,"CC").start();
        System.out.println(futureTask.get());
        //4:线程池
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                3,//核心线程
                10,//最大线程
                5,//空闲线程的存活时间
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(10),//阻塞队列
                Executors.defaultThreadFactory(),//
                new ThreadPoolExecutor.AbortPolicy()//默认拒绝策略
        );
        //4.1
        threadPoolExecutor.execute(new MyRunnable());//Runnable  线程执行但没有返回值
        //4.2
        Future<String> future = threadPoolExecutor.submit(new MyCallable());//Callable   线程执行但有返回值
        System.out.println("返回值:" + future.get());

    }


}
class MyCallable implements Callable<String>{

    @Override
    public String call() throws Exception {
        System.out.println(Thread.currentThread().getName());
        return "我是Callable接口";
    }
}
class MyRunnable implements Runnable{


    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
    }
}
class MyThread extends Thread{

    public MyThread(String name) {
        super(name);
    }

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
    }
}
